package cn.com.tuns.assess.qsb.frame.util.location;

import android.text.TextUtils;
import android.util.Log;
import cn.com.tuns.assess.qsb.frame.ui.MyApplication;
import cn.com.tuns.assess.qsb.frame.util.SharedPreferencesUtil;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;


public class LocationUtil {

    //声明AMapLocationClient类对象
    private AMapLocationClient mLocationClient = null;
    private AMapLocationListener mAMapLocationListener;


    private boolean getAddress;

    private static LocationUtil locationUtil;

    private boolean isLocation;

    private LocationUtil(){

        try
        {
            mLocationClient = new AMapLocationClient(MyApplication.context);
            //异步获取定位结果
            mAMapLocationListener = new AMapLocationListener() {
                @Override
                public void onLocationChanged(AMapLocation amapLocation) {
                    if (amapLocation != null) {
                        if (amapLocation.getErrorCode() == 0) {
                            //解析定位结果
                            String addressTemp = amapLocation.getAddress();
                            if (!TextUtils.isEmpty(addressTemp)) {
                                Log.e("address",addressTemp);
                                SharedPreferencesUtil.save(MyApplication.context, SharedPreferencesUtil.AdressKey, addressTemp);
                            }

                            stopLoction();
                        }
                    }
                }
            };
        }
        catch (Exception e)
        {

        }
    }

    public static LocationUtil getInstanse(){
        if(locationUtil==null)
        {
            locationUtil=new LocationUtil();
        }
        return  locationUtil;
    }


    public void startLoction(){
        stopLoction();
        isLocation=true;

        // 初始化定位参数
        AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
        // 设置返回地址信息，默认为true
        mLocationOption.setNeedAddress(getAddress);
        // 设置定位监听
        mLocationClient.setLocationListener(mAMapLocationListener);
        // 设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        // 设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(2000);
        // 设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
        // 注意设置合适的定位时间的间隔（最小间隔支持为1000ms），并且在合适时间调用stopLocation()方法来取消定位请求
        // 在定位结束后，在合适的生命周期调用onDestroy()方法
        // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
        //启动定位
        mLocationClient.startLocation();
    }

    public void stopLoction(){
        isLocation=false;
        mLocationClient.stopLocation();
    }

    public boolean isGetAddress() {
        return getAddress;
    }

    public void setGetAddress(boolean getAddress) {
        this.getAddress = getAddress;
    }

    public boolean isLocation() {
        return isLocation;
    }
}
